package com.baselibrary.adapter

import android.text.TextUtils
import android.view.View
import com.baselibrary.R
import com.baselibrary.bean.DynamicInfoBean
import com.baselibrary.extension.checkValue
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.ImageUtils
import com.baselibrary.view.SmallVipHeardImgView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.like.LikeButton
import com.like.OnLikeListener


/**
 *@author:hanke
 *2022/3/30
 *desc:
 */
class CommonDynamicAdapter : BaseQuickAdapter<DynamicInfoBean.DataBeanX.DataBean, BaseViewHolder>(R.layout.item_common_dynamic) {
    private var mCallback: Callback? = null
    private var showDelete = false
    private var sourceName = ""
    var mLastPos = -1
    fun setSourceName(sourceName: String) {
        this.sourceName = sourceName
    }

    override fun convert(helper: BaseViewHolder, timeline: DynamicInfoBean.DataBeanX.DataBean) {
        ImageUtils.setImageViewSize(helper.getView(R.id.iv_dynamic_cover), false)
        GlideUtils.loadImg_228(mContext, timeline.cover, helper.getView(R.id.iv_dynamic_cover))
//        requestImage(timeline.cover,helper.getView(R.id.iv_dynamic_cover))

        helper.setText(R.id.tv_dynamic_title, timeline.content.trim())
        val vipHeardImgView = helper.getView<SmallVipHeardImgView>(R.id.iv_dynamic_head)
        if (timeline.user != null) {
            vipHeardImgView.setData(timeline.user.icon, timeline.user.isFans == 1, View.INVISIBLE)
            helper.setText(R.id.tv_dynamic_username, timeline.user.name)
        } else {
            vipHeardImgView.setData(timeline.icon.checkValue(), timeline.isFans == 1)
            helper.setText(R.id.tv_dynamic_username, timeline.name.checkValue())
        }
        helper.setText(R.id.tv_dynamic_like_number, timeline.supportNum.toString())
        val likeStatus: LikeButton = helper.getView(R.id.iv_dynamic_like)
        likeStatus.setOnLikeListener(object : OnLikeListener {
            override fun liked(likeButton: LikeButton?) {
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    mCallback?.onClickLike(mData.indexOf(timeline) + headerLayoutCount, timeline)
                }
            }

            override fun unLiked(likeButton: LikeButton?) {
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    mCallback?.onClickLike(mData.indexOf(timeline) + headerLayoutCount, timeline)
                }
            }
        })
        likeStatus.isLiked = timeline.isSupported
        helper.setVisible(R.id.iv_play, timeline.showType == 2)
        helper.setGone(R.id.tv_dynamic_title, !TextUtils.isEmpty(timeline.content))
        helper.itemView.setOnClickListener {

            mLastPos = helper.adapterPosition
            if (!DoubleClickHelper.isOnDoubleClick()) {
                if (timeline.showType == 2) {
                    goVideoDetail(timeline, helper.getView(R.id.iv_dynamic_cover))
                } else {
                    goImageDetail(timeline, helper.getView(R.id.iv_dynamic_cover))
                }
            }
        }
        helper.setGone(R.id.iv_delete_dynamic, showDelete)
        helper.setGone(R.id.tv_watch_number, showDelete)
        helper.setText(R.id.tv_watch_number, timeline.browseNum.toString())
        helper.addOnClickListener(R.id.iv_delete_dynamic)
    }

    //子频道 图文详情
    private fun goImageDetail(timeline: DynamicInfoBean.DataBeanX.DataBean, view: View) {
        val hashMap = HashMap<String, Any>()
        hashMap["id"] = timeline.id.toString()
        hashMap["source"] = sourceName
        ARouterUtils.jump(ActivityRouter.ACTIVITY.IMAGE_DETAIL, hashMap, view)
    }

    private fun goVideoDetail(timeline: DynamicInfoBean.DataBeanX.DataBean, view: View) {
        val hasMap: HashMap<String, Any> = hashMapOf()
        hasMap["id"] = timeline.id
        hasMap["isOpenComment"] = false
        hasMap["JumpType"] = 0
        hasMap["bid"] = timeline.id.toString()
        hasMap["source"] = sourceName
        ARouterUtils.jump(ActivityRouter.ACTIVITY.READ_VIDEO_DETAIL, hasMap, view)
    }


    fun remove() {
        if (mLastPos != -1) {
            remove(mLastPos)
            mLastPos = -1
        }
    }

    fun setCallback(mCallback: Callback?) {
        this.mCallback = mCallback
    }

    fun setShowDelete(showDelete: Boolean) {
        this.showDelete = showDelete
    }

    interface Callback {
        fun onClickLike(position: Int, item: DynamicInfoBean.DataBeanX.DataBean)
    }
}